package com.example.taigame;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;

import android.app.ProgressDialog;
import android.content.Intent;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Environment;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.GridView;

import com.example.adapter.main_fragment_adapter;
import com.example.model.ListGameType;

public class DemoFragment extends Fragment implements OnItemClickListener {

	private static final String ARG_POSITION = "position";

	private int position;

	public static DemoFragment newInstance(int position) {
		DemoFragment f = new DemoFragment();
		Bundle b = new Bundle();
		b.putInt(ARG_POSITION, position);
		f.setArguments(b);
		return f;
	}

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		position = getArguments().getInt(ARG_POSITION);
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		View view = inflater.inflate(R.layout.gridview_main, container, false);
		GridView g = (GridView) view.findViewById(R.id.gridView1);
		ArrayList<ListGameType> listGame = MyApp.listGameType;
		if (MyApp.listGameType.size() > position) {

			if (MyApp.listGameType.get(position).getListGame() != null
					&& MyApp.listGameType.get(position).getListGame().size() > 0) {
				g.setAdapter(new main_fragment_adapter(getActivity(), position));
				g.setOnItemClickListener(this);
			} else {

			}
		}
		return view;
	}

	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position,
			long id) {
		MyApp.currentSelectedGame = MyApp.listGameType.get(this.position).getListGame().get(position);
		Intent intent = new Intent(MyApp._currentActivity, GameDetail.class);
		intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
		MyApp._currentActivity.startActivity(intent);
	}

}